package com.splic.common.core.domain;

import com.splic.common.core.DeviceContext;
import com.splic.common.exception.BusinessException;

public class ResponseObj<T> {

    /**
     * 是否成功
     */
    private boolean success;
    /**
     * 状态码
     */
    private String code = "0";
    /**
     * 提示信息
     */
    private String message;

    /**
     * 是否成功
     */
    private String deviceIdentify;
    /**
     * 数据集合
     */
    private T result;
    private static final String SUCCESS_MESSAGE = "操作成功";
    public static final ResponseObj SUCCESS = SUCCESS();

    public static final ResponseObj SUCCESS() {
        ResponseObj responseObj = new ResponseObj();
        responseObj.success = true;
        responseObj.code = "1";
        responseObj.message = "操作成功";
        responseObj.deviceIdentify = DeviceContext.getDeviceIdentify();
        return responseObj;
    }

    public static final <T> ResponseObj<T> SUCCESS(T result) {
        ResponseObj responseObj = SUCCESS();
        responseObj.result = result;
        return responseObj;
    }

    public static final ResponseObj ERROR(String msg) {
        return new ResponseObj(msg, "0");
    }

    public static final ResponseObj ERROR(String msg, String code) {
        return new ResponseObj(msg, code);
    }

    public static final <T> ResponseObj<T> ERROR(String msg, String code, T result) {
        ResponseObj responseObj = new ResponseObj(msg, code);
        responseObj.result = result;
        return responseObj;
    }


    public static final <T> ResponseObj<T> WARN(String msg, T result) {
        ResponseObj responseObj = new ResponseObj(msg, "110");
        responseObj.result = result;
        return responseObj;
    }

    public static <T> T getResponseData(ResponseObj<T> res) {
        if (null == res) {
            throw new BusinessException("get Api ResponseObj is null !");
        } else if (!res.isSuccess()) {
            throw new BusinessException(res.getMessage());
        } else {
            return res.getResult();
        }
    }

    public static <T> T getResponseNotNull(ResponseObj<T> res) {
        T t = getResponseData(res);
        if (t == null) {
            throw new BusinessException("get Api responseObj.Result is null !");
        } else {
            return t;
        }
    }

    public static <T> T getResponseNotNull(ResponseObj<T> res, String cause) {
        T t = getResponseData(res);
        if (t == null) {
            throw new BusinessException(cause);
        } else {
            return t;
        }
    }



    protected ResponseObj() {
        this.success = true;
    }

    protected ResponseObj(String msg, String code) {
        this.success = false;
        this.message = msg;
        this.code = code;
        this.deviceIdentify = DeviceContext.getDeviceIdentify();
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return this.result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public String getDeviceIdentify() {
        return deviceIdentify;
    }

    public void setDeviceIdentify(String deviceIdentify) {
        this.deviceIdentify = deviceIdentify;
    }

    public String toString() {
        return "ResponseObj{success=" + this.success + ", code='" + this.code + '\'' + ", message='" + this.message + '\'' + ", deviceIdentify='" + this.deviceIdentify + '\'' + ", result=" + this.result + '}';
    }
}